<?php
/**
**华侨城验证消费通知
*/
include("/var/www/html/new/d/module/api/HuaQiaoCheng/des.class.php");
include('/var/www/html/new/d/module/api/function.class.php');
if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}

$PFT_infunction = new AllFunction();
$PFT_infunction->logfile('15|'.$_REQUEST['xmlContent'],'checkNote');
/* $arr = '<?xml version="1.0" encoding="UTF-8"?><OTM><Head><Version>V2.1.0</Version><SequenceId>Consume_36626577</SequenceId><TimeStamp>2015-11-27 15:38:06</TimeStamp><DistributorId>distri735</DistributorId><TransactionCode>R0</TransactionCode><Signed>Y2E2YTlmMjI2NmNhMGMwNDBjMDFiZWQ5NGEwZWU0Nzg=</Signed></Head><Body><![CDATA[N0YwNTk5N0EwQTA4QzQxRTA4NkJEQ0IzQkE1NDg5MkYyMUVENTZBRjg5REE5RTBBQzVFRjA1NURBQTI0MjQxMDFBMzNFNzZCODFBQjBBNzk2NEZERjFDRjNFRDM2MzI2NTIwMTRFNUJDMDgxMUM1NDZFRDY5MzNCNDI0MEFDNUZENTQ1QUExRjk0QUQyMTc2QjIxQTcyMkE3MUZFNDJENjIwNDJGQjhGQjBERkFDRTkyMTgwMjZCRjY0MkQxQUNCMzM2QTYzRkRGQUI3MUUwMUFENUQ5NjRFOUNCQzk2NTVCMDBDNDQyODAwNzhENzJGOTk0MkREQTAzRjg5QzczQTEzODdGNUNCQTBEREVENUUyQjczREFDNDE1MjJGRjE5M0ZCMzlCMUQ0NEM5NjIxRjc5NjdBRkY3M0M2RDBFQTU4RjREMzA4NzhDQTYyOTkwNEQ0QzRDNTNEQkE1QUNGNDEyNUVDMzI5M0Q1Q0UzQkM4NjMxMjFDODFFRkYzOEEwNUM1MjhEQzc5MTI5NUE1NzQ4NTE4MEI1QTQyMUY2NDIzRjAwQ0M2Q0RCQ0ZCOENEOTFCQUM0OEZFRjkwMkIzMzY1NjREQTNDMjVBQzIzODMxMUE5QjlEREZEQkM3QjUwODk1NzhENzE2MzMzM0ZDMjIyNjRGOEQ0QzE2QzJEMEE3QjA2MzlEMDZERkY3MTVEMTY4MTlEMzg1MTZBQjg2QzMzMjI5QjUxNjI1QTc0NTNBMkNENEZGNENFNDE5OEFEMkM4NUQwQzVBRkNEMDQ2OEZGQ0QzRDhCNzI4RUNGQTc1MUM2N0MwMTlDM0NDODgxRTI2MzNCRkU1N0NDNEIxMUEzQzI5RkQ2Q0VCNDdBQTkxMENFRUUwNjFBOUE3Q0ZBNTU4QkE1REMxNkE4OEM5MTY0REQxNEEyQzcwRUM1RDkxMjA1Q0U4MjlBREIxQ0IxNzU2ODhGQTRDMjZBN0NGRDE1OEMzQ0JENzk1QkJGMUMxMUFFMkZGRDMxNERCNjA1NEE2MUJCN0I3QjZFRjUyQUZFRTQzNTBDNDNCRDk1QTkwQjY4OEUzRDk2NTFBMENGMUM5Q0U4QkNFMEM5RUU2RA==]]></Body></OTM>'; */
// print_r(simplexml_load_string( $txt, 'SimpleXMLElement', LIBXML_NOCDATA ));exit;
// exit;

$arr = $_REQUEST['xmlContent'];
$arr_xml = simplexml_load_string( $arr, 'SimpleXMLElement', LIBXML_NOCDATA );
// print_r($arr_xml);exit;
$DistributorId = (string)$arr_xml->Head->DistributorId; //OTA编号
$SequenceId = (string)$arr_xml->Head->SequenceId; //序列号
$TransactionCode = (string)$arr_xml->Head->TransactionCode;  //交易码
$body = (string)$arr_xml->Body;
$sel = "select signkey from uu_qunar_use where DockingMode=6 and supplierIdentity='$DistributorId'  limit 1";
$GLOBALS['le']->query($sel);
$GLOBALS['le']->fetch_assoc();		;	  
$secretKey = $GLOBALS['le']->f('signkey');

$hqc_des = new Crypt3Des($secretKey);
$body_des = $hqc_des->decrypt($body);
$body_xml = simplexml_load_string($body_des);
// print_r($body_xml);exit;
$OtaOrder = (string)$body_xml->OutOrderId; //OTA订单编号。
$OtmOrderId = (string)$body_xml->OtmOrderId; //运营商平台的订单编号
$ConsumeTimes = (string)$body_xml->ConsumeTimes; //消费数量
$ConsumeDate = (string)$body_xml->ConsumeDate; //消费时间

$sel = "select pftOrder from all_api_order where tempOrder='$OtaOrder' and apiOrder='$OtmOrderId' and coopB=15 limit 1";
$GLOBALS['le']->query($sel);
if(!$GLOBALS['le']->fetch_assoc()){
	$StatusCode = 300024 ;
	$Message = '订单不存在';
	echo PackageData($TransactionCode,$SequenceId,$StatusCode,$Message,$secretKey,$DistributorId);
	exit;
}

$pftOrder = $GLOBALS['le']->f('pftOrder');
$pft_order_xml = $PFT_infunction->Select_PFTorder($pftOrder);
$UUsalerid  = (string)$pft_order_xml->Rec->UUsalerid;
$UUcode  = (string)$pft_order_xml->Rec->UUcode;
$chkIns = 499;
if((int)$pft_order_xml->Rec->UUstatus==2) $chkIns = 498;
$r = $PFT_infunction->Unified_Check_IN($UUsalerid,$UUcode,$ConsumeDate,$chkIns);
if($r == 100){
	$selorder = "update all_api_order  set oStatus=1 where pftOrder='$pftOrder' limit 1";
	$GLOBALS['le']->query($selorder);
	$PFT_infunction->logfile('15|'.$pftOrder."|".$OtaOrder."|".$OtmOrderId."|".$r,"check_s");	
	$StatusCode = 200 ;
	$Message = '通知成功';
	echo PackageData($TransactionCode,$SequenceId,$StatusCode,$Message,$secretKey,$DistributorId);
	exit;
	
}else{
	$PFT_infunction->logfile('15|'.$pftOrder."|".$OtaOrder."|".$OtmOrderId."|".$r,"check_f");
	$StatusCode = $r;
	$Message = '通知失败';
	echo PackageData($TransactionCode,$SequenceId,$StatusCode,$Message,$secretKey,$DistributorId);
	exit;
}


function PackageData($TransactionCode,$SequenceId,$StatusCode,$Message,$DistributorId){
	$sign = base64_encode(md5($SequenceId.$DistributorId));
	$time = date('Y-m-d H:i:s');
	$reponse_heder = '<?xml version=”1.0” encoding=”UTF-8”?><OTM><Head><Version>2.1.0</Version><SequenceId>'.$SequenceId.'</SequenceId><TimeStamp>'.$time.'</TimeStamp><DistributorId>'.$DistributorId.'</DistributorId><TransactionCode>'.$TransactionCode.'</TransactionCode><StatusCode>'.$StatusCode.'</StatusCode><Message>'.$Message.'</Message><Signed>'.$sign.'</Signed></Head></OTM>';
	return $reponse_heder;
}